$( document ).ready( function() {
	// Take over the submit action
	// attached to the body as the ajax call will replace the form 
	// and otherwise the onsubmit handler will be lost
	$('body').on('submit','#frmRegister', function(e){

		// Update submit text to indicate something is happening
		//$('.submit :input').val('Loading');
        $('.button-icon span').removeClass('icon-mail');
        $('.button-icon span').addClass('icon-mail-load');

        $.ajax({
            type: "POST",
            url: $(this).attr('action'),
            data: $(this).serialize(),
			dataType: "html"
            //dataType: "json"
        })
        .done(function(data) {
			$('#msg-error').remove();
			$('#msg-success').html(data);
			$('#register-success').modal('show');
			
			//$('.box-actions').after('<div id="msg-error"><p>'+ data.msg +'</p></div>');
            $('#fullname').val('');
			$('#phone').val('');
			$('#birthday').val('');
			$('#email').val('');  
			$.get('/register/createToken', function(regis_token) {
				$('#token').val(regis_token);
			});
            $('.button-icon span').removeClass('icon-mail-load');
            $('.button-icon span').addClass('icon-mail');
        })
        .fail(function(data) {
            $('#msg-error').remove();
            $('.box-actions').after('<div id="msg-error"><p>System error</p></div>');
        });

        // return false to stop the page from posting normally
        return false;
	});
});